//index.js
//获取应用实例
var tcity = require("../../../utils/citys");
var call = require("../../../utils/util");
var app = getApp();
Page({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    shName:"",
    shPhone:"",
    shAddress:"",
    mr:false,
    noneid:true,
    address_id:"",
    pNumber:"",
    cNumber:"",
    aNumber:""
  },
  returnMenu(){
    wx.navigateBack({
      data:1,
    })
  },
  changMr:function(e){
    // console.log(this.data.mr);
    this.setData({
      mr:!this.data.mr
    })
  },
  bindChange: function(e) {
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    if(val[0] != t[0]){
      // console.log('province no ');
      const citys = [];
      const countys = [];
      for (let i = 0 ; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0 ; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }
      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys:citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys:countys,
        values: val,
        value:[val[0],0,0]
      })
      return;
    }
    if(val[1] != t[1]){
      // console.log('city no');
      const countys = [];
      for (let i = 0 ; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }
      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys:countys,
        values: val,
        value:[val[0],val[1],0]
      })
      return;
    }
    if(val[2] != t[2]){
      // console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }
    

  },
  
  open:function(){
    this.setData({
      condition:!this.data.condition
    })
  },
  showName(e){
    this.setData({
      shName:e.detail.value
    })
  },
  showPhone(e){
    this.setData({
      shPhone:e.detail.value
    })
  },
  showAddress(e){
    this.setData({
      shAddress:e.detail.value
    })
  },
  //点击保存，验证表单
  saveAdress:function(){
    if(this.data.shName==""){
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 1500
      })
    }else{
      if(this.data.shPhone==""){
        wx.showToast({
          title: '手机号不能为空',
          icon: 'none',
          duration: 1500
        })
      }else if(!/^1[3456789]\d{9}$/.test(this.data.shPhone)){
        wx.showToast({
          title: '手机号格式错误',
          icon: 'none',
          duration: 1500
        })
      }else if(this.data.shAddress==""){
        wx.showToast({
          title: '详细地址不能为空',
          icon: 'none',
          duration: 1500
        })
      }else{
        let procince="";
        let city="";
        let area="";
        let detailed="";
        let phone="";
        let name="";
        //获取地址信息
        procince=this.data.cityData[this.data.values[0]].code; 
        city=this.data.cityData[(this.data.values[0])].sub[this.data.values[1]].code;
        area=this.data.cityData[(this.data.values[0])].sub[this.data.values[1]].sub[this.data.values[2]].code;
        detailed=this.data.shAddress;
        phone=this.data.shPhone;
        name=this.data.shName;
        // console.log("省:"+procince); 
        // console.log("市:"+city); 
        // console.log("区:"+area); 
        // console.log("详细地址:"+detailed); 
        // console.log("姓名"+name);
        // console.log("电话"+phone);
        //请求接口添加地址
        let token = wx.getStorageSync('token');
        call.request(
          'api/userAddressAddModify',
          {
            'token':token,
            'phone':phone,
            'procince':procince,
            'city':city,
            'area':area,
            'name':name,
            'detailed':detailed
          },
          this.addsuccess,
          this.error);  
      }
    }
  },
  //修改地址
  resetAdress:function(){
    let id=this.data.address_id;
    let temp=false;

    if(this.data.shName==""){
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 1500
      })
    }else{
      if(this.data.shPhone==""){
        wx.showToast({
          title: '手机号不能为空',
          icon: 'none',
          duration: 1500
        })
      }else if(!/^1[3456789]\d{9}$/.test(this.data.shPhone)){
        wx.showToast({
          title: '手机号格式错误',
          icon: 'none',
          duration: 1500
        })
      }else if(this.data.shAddress==""){
        wx.showToast({
          title: '详细地址不能为空',
          icon: 'none',
          duration: 1500
        })
      }else{
        let procince="";
        let city="";
        let area="";
        let detailed="";
        let phone="";
        let name="";

        //判断values是否为[0,0,0] 
        for(let i=0;i<this.data.values.length;i++){
          if(this.data.values[i]!=0){temp=true}
        }
        //没改变为false，改了为true
        console.log(this.data.values);
        console.log(temp);
        
        if(temp==false){
            procince=this.data.pNumber;
            city=this.data.cNumber;
            area=this.data.aNumber;
        }else{
          //重新获取
          procince=this.data.cityData[this.data.values[0]].code; 
          city=this.data.cityData[(this.data.values[0])].sub[this.data.values[1]].code;
          area=this.data.cityData[(this.data.values[0])].sub[this.data.values[1]].sub[this.data.values[2]].code;
        }
        detailed=this.data.shAddress;
        phone=this.data.shPhone;
        name=this.data.shName;

        // console.log("省:"+procince); 
        // console.log("市:"+city); 
        // console.log("区:"+area); 
        // console.log("详细地址:"+detailed); 
        // console.log("姓名"+name);
        // console.log("电话"+phone);

        let token = wx.getStorageSync('token');
       
        call.request(
          'api/userAddressAddModify',
          {
            'token':token,
            'phone':phone,
            'procince':procince,
            'city':city,
            'area':area,
            'name':name,
            'detailed':detailed,
            'address_id':id
          },
          this.resetsuccess,
          this.error);  
      }
    }
  },
  resetsuccess(msg){
    // console.log("修改成功");
    let id=this.data.address_id;
    let token = wx.getStorageSync('token');
    if(this.data.mr==true){
      call.request(
        'api/userAddressDfault',
        {
          'token':token,
          'id':id
        },
        this.szsuccess,
        this.error); 
    }else{
      wx.navigateBack({
        delta: 1,
      });
    }
  },

  //添加成功返回函数
  addsuccess(msg){
    // console.log("添加成功");
    console.log(msg.other.add_address_id);
    let token = wx.getStorageSync('token');
    if(this.data.mr==true){
      call.request(
        'api/userAddressDfault',
        {
          'token':token,
          'id':msg.other.add_address_id
        },
        this.szsuccess,
        this.error); 
    }else{
      wx.navigateBack({
        delta: 1,
      });
    }
  },
  szsuccess(msg){
    // console.log(msg);
    wx.navigateBack({
      delta: 1,
    })
  },
  error(err){
    // console.log(err);
  },
 
  onLoad: function (options) {
    var that = this;
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];
    for(let i=0;i<cityData.length;i++){
      provinces.push(cityData[i].name);
    }
    for (let i = 0 ; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    for (let i = 0 ; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    that.setData({
      'provinces': provinces,
      'citys':citys,
      'countys':countys,
      'province':cityData[0].name,
      'city':cityData[0].sub[0].name,
      'county':cityData[0].sub[0].sub[0].name
    })
    // console.log('初始化完成');
    //判断是否携带参数，有参数就进行参数替换
    that.address_id=options.id;
    if(that.address_id==undefined){
      that.setData({
        noneid:true
      })
    }else{
      //获取参数，赋值
      that.name=options.name;
      that.phone=options.phone;
      that.status=options.status;
      that.address=options.address;
      that.pNumber=options.pNumber;
      that.cNumber=options.cNumber;
      that.aNumber=options.aNumber;
      that.setData({
        noneid:false,
        address_id:that.address_id
      });
    
    //地址处理
    let arr=[];
    let arr2=[];
    let status=false;
    if(that.status=="0"){status=false;}else{status=true;}
    //因为此处以空格会导致详细地址拆分不全
    arr=that.address.split(" ");
    arr2=arr[0].split("-");
    arr.splice(0,1);
    arr=arr.filter((value)=>{return value!=""});   //去空
    arr=arr.join('');
    that.setData({
      shName:that.name,
      shPhone:that.phone,
      shAddress:arr,
      mr:status,
      province:arr2[0],
      city:arr2[1],
      county:arr2[2],
      pNumber:that.pNumber,
      cNumber:that.cNumber,
      aNumber:that.aNumber
    });
    }
  },

})
